<template>
  <div v-if="reloaded" class="formFontStyle" :class="`${editable ? 'editable' : 'editable-no'}`">
    <a-form :form="form" :layout="formLayout" :labelCol="formItemLayout.labelCol" :wrapperCol="formItemLayout.wrapperCol">
      <a-row v-if="isReward && formFields" :gutter="24">
        <template v-if="sortedKeys">
          <template v-for="key in sortedKeys">
            <div v-if="!(formFields[key].isHidden === true)" :key="key">
              <a-col :lg="12" :md="24">
                <XqItem
                  ref="XqItem"
                  :form="form"
                  :dataSource="dataSource"
                  :item="formFields[key]"
                  :dynamicData="dynamicData"
                  :editable="editable"
                  :validator="getValidator(key)"
                  @change="XqItemChange($event, formFields[key], dataSource, dynamicData)"
                  @inputKeydown="onInputKeydown($event, formFields[key])"
                />
              </a-col>
            </div>
          </template>
        </template>
        <template v-else>
          <div v-for="item in formFields" :key="item.model">
            <a-col :lg="12" :md="24">
              <XqItem
                ref="XqItem"
                :form="form"
                :dataSource="dataSource"
                :item="item"
                :dynamicData="dynamicData"
                :editable="editable"
                :validator="getValidator(item.model)"
                @change="XqItemChange($event, item, dataSource, dynamicData)"
                @inputKeydown="onInputKeydown($event, item)"
              />
            </a-col>
          </div>
        </template>
      </a-row>
    </a-form>
    <div v-if="showBtns" v-show="editable" class="DynamicForm">
      <a-space>
        <a-button v-if="remove" type="danger" ghost @click="onRemove">删 除</a-button>
        <a-button @click="cancel">取 消</a-button>
        <a-button type="primary" @click="save">保 存</a-button>
      </a-space>
    </div>
  </div>
</template>
<script>
import XqFormMixin from "xqjr-plugin-form/mixins/XqFormMixin";
const FormMixin = XqFormMixin();
import moment from "moment";
export default {
  mixins: [FormMixin],
  props: {
    sortedKeys: {
      type: Array,
      default: () => null
    },
    remove: {
      type: Boolean,
      default: false
    },
    dataSource: {
      type: Object,
      default: () => {}
    },
    editable: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: Boolean,
      default: true
    },
    dynamicData: {
      type: Object,
      default: () => {}
    },
    showBtns: {
      type: Boolean,
      default: true
    },
    formLayout: {
      type: String,
      default: "horizontal"
    }
  },
  data() {
    return {
      isReward: true,
      spouseInfo: false,
      reloaded: true
    };
  },
  computed: {
    formItemLayout() {
      return this.formLayout === "horizontal"
        ? {
            labelCol: { span: 10 },
            wrapperCol: { span: 12 }
          }
        : {};
    }
  },
  watch: {
    // dataSource: {
    //   handler(newVal) {
    //     this.reloaded = false;
    //     this.$nextTick(() => {
    //       this.reloaded = true;
    //       console.log(JSON.stringify(newVal));
    //     });
    //   },
    //   deep: true
    // }
  },
  methods: {
    vilidateRetirementDate(rule, value, callback) {
      const form = this.form;
      if (this.$attrs.basicData.birthday) {
        if (moment(value).valueOf() < moment(this.$attrs.basicData.birthday).valueOf()) {
          callback("退休日期不能大于出生日期");
        } else {
          callback();
        }
      } else {
        callback();
      }
    },
    getDataRules() {
      this.getData()
        .then((res) => {
          console.log("debug log --> ", res);
        })
        .catch((err) => {
          console.log("debug log --> ", err);
        });
    },
    onInputKeydown(evt, item) {
      if (evt.code === "Enter") {
        this.$emit("keydownEnter", evt.target.value, item);
      }
    },
    XqItemChange(val, item, dataSource, dynamicData) {
      if (item.type === "select_add") {
        this.selectAddValueDict[item.model] = val;
      } else if (item.model === "rewardPunish") {
        this.isReward = false;
        this.form.rewardType = null;
        this.$emit("itemChange", val);
        this.isReward = true;
      } else if (item.model === "birthday") {
        this.$emit("itemChange", val);
      } else {
        this.$emit("itemChange", val, item);
      }
    },
    async saveActive() {
      const dd = await this.getData()
        .then((res) => {
          Object.keys(res).forEach((element) => {
            if (res[element] === "true") {
              res[element] = true;
            }
            if (res[element] === "false") {
              res[element] = false;
            }
            if (res[element] && res[element]._isAMomentObject) {
              if (element === "birthday") {
                res[element] = moment(res[element]).format("YYYY-MM-DD") + " 00:00:00";
              } else {
                const format = this.formFields[element].customize.format;
                res[element] = moment(res[element]).format(format || "YYYY-MM-DD");
              }
            }
          });
          return res;
        })
        .catch((err) => {
          return {
            success: false,
            error: err
          };
        });
      return dd;
    },
    save() {
      this.getData()
        .then((res) => {
          Object.keys(res).forEach((element) => {
            if (res[element] === "true") {
              res[element] = true;
            }
            if (res[element] === "false") {
              res[element] = false;
            }
            if (res[element] && res[element]._isAMomentObject) {
              if (element === "birthday") {
                res[element] = moment(res[element]).format("YYYY-MM-DD") + " 00:00:00";
              } else {
                const format = this.formFields[element].customize.format;
                res[element] = moment(res[element]).format(format || "YYYY-MM-DD");
              }
            }
          });

          this.$emit("change", res);
        })
        .catch((err) => {
          this.$emit("change");
          console.log("debug log --> ", err);
        });
    },
    onRemove() {
      this.$emit("remove");
    },
    cancel() {
      this.$emit("update:editable", false);
      this.$emit("cancel");
    },
    onValuesChangeCustom(values) {}
  }
};
</script>

<style lang="scss" scoped>
.DynamicForm {
  text-align: center;
}
::v-deep .ant-form-item-control {
  textarea {
    height: 104px !important;
    min-height: 104px !important;
  }
}
</style>
<style lang="scss">
.formFontStyle {
  label {
    font-weight: 400 !important;
  }
}
</style>
